package ciyuan.Block.Machine.st;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class StTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(2);
    public ContainerProgress containerProgress = new ContainerProgress();

    public StTileEntity() {
        super(TileEntityRegisterManager.st.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc6");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new StContainer(ContainerRegistry.stContainer.get(), sycID, inventory, getBlockPos(), containerProgress);
    }

    @Override
    public void load(BlockState state, CompoundNBT nbt) {
        if (!hasLevel()) {
            this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
            this.inventory.setItem(1,ItemStack.of(nbt.getCompound("item2")));
            if (ItemStack.of(nbt.getCompound("water")).getCount()!=0) {
                this.containerProgress.set(0, ItemStack.of(nbt.getCompound("water")).getCount());
            }
            System.out.println("读取水量:" + ItemStack.of(nbt.getCompound("water")).getCount());
        }


        super.load(state, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemStack = this.inventory.getItem(0).copy();
        ItemStack itemStack2 = this.inventory.getItem(1).copy();
        ItemStack water =  new ItemStack(ItemRegisterManager.Shit.get());
        water.setCount(this.containerProgress.get(0));
        compound.put("item", itemStack.serializeNBT());
        compound.put("item2", itemStack2.serializeNBT());
        compound.put("water",water.serializeNBT() );
        System.out.println("保存水量：" + water.getCount());
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (!this.inventory.getItem(0).isEmpty()) {
            if (this.inventory.getItem(0).getItem().getDescriptionId().equals(Items.WATER_BUCKET.getDescriptionId())) {
                this.inventory.setItem(0,new ItemStack(Items.BUCKET));
                this.containerProgress.set(0,this.containerProgress.get(0)+5);
            } else if (this.inventory.getItem(0).getItem().getDescriptionId().equals(ItemRegisterManager.stonepot2.get().getDescriptionId())) {
                this.inventory.setItem(0,new ItemStack(ItemRegisterManager.stonepot.get()));
                this.containerProgress.set(0,this.containerProgress.get(0)+3);
            }
        }

        if (!this.inventory.getItem(1).isEmpty()) {
            if (this.inventory.getItem(1).getItem().getDescriptionId().equals(Items.BUCKET.getDescriptionId()) && this.containerProgress.get(0)>=5) {
                this.inventory.setItem(1,new ItemStack(Items.WATER_BUCKET));
                this.containerProgress.set(0,this.containerProgress.get(0)-5);
            } else if (this.inventory.getItem(1).getItem().getDescriptionId().equals(ItemRegisterManager.stonepot.get().getDescriptionId()) && this.containerProgress.get(0)>=3 ) {
                this.inventory.setItem(0,new ItemStack(ItemRegisterManager.stonepot2.get()));
                this.containerProgress.set(0,this.containerProgress.get(0)-3);
            }
        }

    }

}
